package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 游戏面板
 *
 * @author zl
 */
public class GamePanel
        extends JPanel {
    BufferedImage bg;
    MyPlane myPlane = new MyPlane();
    EnemyPlane fire = new EnemyPlane();
    List<EnemyPlane> enemyPlanes = new ArrayList<EnemyPlane>();
    List<Bullet> fs = new ArrayList<>();
    int score;
    boolean gameOver = false;
    int power = 1;

    public void action() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    if (!gameOver) {
                        epEnter();
                        epMove();
                        shoot();
                        fireMove();
                        shootEp();
                        hit();
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    repaint();
                }
            }
        }.start();
    }

    private void hit() {
        for (int i = 0; i < enemyPlanes.size(); i++) {
            EnemyPlane e = enemyPlanes.get(i);
            if (e.hitBy(myPlane)) {
                enemyPlanes.remove(e);
                myPlane.hp--;
                power = 1;
                score += 10;
                if (myPlane.hp <= 0) {
                    gameOver = true;
                }
            }
        }
    }

    private void shootEp() {
        for (int i = 0; i < fs.size(); i++) {
            Bullet f = fs.get(i);
            bang(f);
        }
    }

    private void bang(Bullet f) {
        for (int i = 0; i < enemyPlanes.size(); i++) {
            EnemyPlane e = enemyPlanes.get(i);
            if (e.shootBy(f)) {
                if (e.type!=1) {
                    e.hp--;
                    if (e.hp <= 0) {
                        if (e.type == 6) {
                            power++;
                            if (power > 3) {
                                if (myPlane.hp < 5) {
                                    myPlane.hp++;
                                }
                                power = 3;
                            }
                        }
                        enemyPlanes.remove(e);
                        score += 10;
                    }
                }
                fs.remove(f);

            }
        }
    }

    private void fireMove() {
        for (int i = 0; i < fs.size(); i++) {
            Bullet f = fs.get(i);
            f.move();
        }
    }

    int findex = 0;

    private void shoot() {
        findex++;
        if (findex >= 10) {
            if (power == 1) {
                Bullet bullet2 = new Bullet(myPlane.x + 45, myPlane.y - 20, 1);
                fs.add(bullet2);
            } else if (power == 2) {
                Bullet bullet = new Bullet(myPlane.x + 15, myPlane.y, 1);
                fs.add(bullet);
                Bullet bullet1 = new Bullet(myPlane.x + 75, myPlane.y, 1);
                fs.add(bullet1);
            } else if (power == 3) {
                Bullet bullet2 = new Bullet(myPlane.x + 45, myPlane.y - 20, 1);
                fs.add(bullet2);
                Bullet bullet = new Bullet(myPlane.x + 15, myPlane.y, 0);
                fs.add(bullet);
                Bullet bullet1 = new Bullet(myPlane.x + 75, myPlane.y, 2);
                fs.add(bullet1);
            }
            findex = 0;
        }
    }

    private void epMove() {
        for (int i = 0; i < enemyPlanes.size(); i++) {
            EnemyPlane e = enemyPlanes.get(i);
            e.move();
        }
    }

    int index = 0;

    private void epEnter() {
        index++;
        if (index >= 20) {
            EnemyPlane e = new EnemyPlane();
            enemyPlanes.add(e);
            index = 0;
        }
    }

    public GamePanel(GameFrame frame) {
        setBackground(Color.black);
        bg = GetPicture.getImg("/picture/mapback1.png");
        MouseAdapter adapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (gameOver) {
                    myPlane = new MyPlane();
                    gameOver = false;
                    score = 0;
                    enemyPlanes.clear();
                    fs.clear();
                    Random random = new Random();
                    int index = random.nextInt(2) + 1;
                    bg = GetPicture.getImg("/picture/mapback" + index + ".png");
                    int index2 = random.nextInt(5) + 1;
                    myPlane.img = GetPicture.getImg("/picture/Plane0" + index2 + ".png");
                    int index3 = random.nextInt(2) + 1;
                    fire.img = GetPicture.getImg("/picture/bullet_0" + index3 + ".png");
                    repaint();
                }
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                int mx = e.getX();
                int my = e.getY();
                if (!gameOver) {
                    myPlane.moveToMouse(mx, my);
                }
                repaint();
            }
        };
        addMouseListener(adapter);
        addMouseMotionListener(adapter);
        KeyAdapter kd = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if (!gameOver) {
                    if (keyCode == KeyEvent.VK_UP) {
                        myPlane.moveUp();
                    } else if (keyCode == KeyEvent.VK_DOWN) {
                        myPlane.moveDown();
                    } else if (keyCode == KeyEvent.VK_LEFT) {
                        myPlane.moveLeft();
                    } else if (keyCode == KeyEvent.VK_RIGHT) {
                        myPlane.moveRight();
                    }
                }
                repaint();
            }
        };
        frame.addKeyListener(kd);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bg, 0, 0, 512, 768, null);
        g.drawImage(myPlane.img, myPlane.x, myPlane.y, myPlane.w, myPlane.h, null);
        for (int i = 0; i < enemyPlanes.size(); i++) {
            EnemyPlane enemyPlane = enemyPlanes.get(i);
            g.drawImage(enemyPlane.img, enemyPlane.x, enemyPlane.y, enemyPlane.w, enemyPlane.h, null);
        }
        for (int i = 0; i < fs.size(); i++) {
            Bullet bullet = fs.get(i);
            g.drawImage(bullet.img, bullet.x, bullet.y, bullet.w, bullet.h, null);
        }
        g.setColor(Color.white);
        g.setFont(new Font("楷体", Font.BOLD, 20));
        g.drawString("分数:" + score, 10, 30);
        for (int i = 0; i < myPlane.hp; i++) {
            g.drawImage(myPlane.image, 350 + i * 30, 5, 30, 30, null);
        }
        if (gameOver) {
            g.setColor(Color.red);
            g.setFont(new Font("楷体", Font.BOLD, 35));
            g.drawString("GAMEOVER！ 菜鸡!!!", 75, 300);
            g.setColor(Color.green);
            g.setFont(new Font("楷体", Font.BOLD, 20));
            g.drawString("点击屏幕任意位置重新开始", 100, 350);

        }
    }
}
